import { Service } from "egg";
import { PrivilegesTypeProps } from "../model/PrivilegesType";
import { PrivilegesProps } from "../model/Privileges";

/**
 * Test Service
 */
export default class PrivilegesTypeService extends Service {
  /**
   * 根据传入名称查询
   * @param {string} privileges_name 名称
   */
  public findByName(privileges_name) {
    return this.ctx.model.Privileges.findOne({ privileges_name });
  }

  /**
   * 查找列表
   */
  public findList() {
    const { ctx } = this;
    return ctx.model.PrivilegesType.aggregate([
      {
        $lookup: {
          from: "privileges",
          localField: "_id",
          foreignField: "privileges_type_id",
          as: "privileges",
        },
      },
    ]);
  }

  /**
   * 添加数据
   * @param data 测试数据
   */
  public async add(data) {
    const { ctx } = this;
    const _data: Partial<PrivilegesProps> = {
      ...data,
    };
    return await ctx.model.Privileges.create(_data);
  }

  /**
   * 根据传入名称查询
   * @param {string} privileges_name 名称
   */
  public findByNameType(privileges_name) {
    return this.ctx.model.Privileges.findOne({ privileges_name });
  }

  /**
   * 添加数据
   * @param data 测试数据
   */
  public async addType(data) {
    const { ctx } = this;
    const _data: Partial<PrivilegesTypeProps> = {
      ...data,
    };
    return await ctx.model.PrivilegesType.create(_data);
  }
}
